package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.TEquipmentBo;
import com.ruoyi.system.domain.vo.TEquipmentVo;
import com.ruoyi.system.domain.dto.TEquipmentDTO;
import com.ruoyi.system.domain.TEquipment;
import com.ruoyi.system.mapper.TEquipmentMapper;
import com.ruoyi.system.service.ITEquipmentService;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 设备 Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-13
 */
@RequiredArgsConstructor
@Service
public class TEquipmentServiceImpl implements ITEquipmentService {

    private final TEquipmentMapper baseMapper;

    /**
     * 查询设备
     */
    @Override
    public TEquipmentVo queryById(Long id){
        TEquipmentVo tEquipmentVo = baseMapper.selectVoById(id);
        return tEquipmentVo;
    }

    /**
     * 查询设备 列表
     */
    @Override
    public TableDataInfo<TEquipmentVo> queryPageList(TEquipmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TEquipment> lqw = buildQueryWrapper(bo);
        Page<TEquipmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

     @Override
     public  TableDataInfo<TEquipmentDTO> queryMyPageList(TEquipmentDTO dto, PageQuery pageQuery){
          Page<TEquipmentDTO> build = pageQuery.build();
          List<TEquipmentDTO> list = baseMapper.pageMy(build, dto);
          Page<TEquipmentDTO> objectPage = build.setRecords(list);
         return TableDataInfo.build(objectPage);
    }

    /**
     * 查询设备 列表
     */
    @Override
    public List<TEquipmentVo> queryList(TEquipmentBo bo) {
        LambdaQueryWrapper<TEquipment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TEquipment> buildQueryWrapper(TEquipmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TEquipment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquipmentTypeId() != null, TEquipment::getEquipmentTypeId, bo.getEquipmentTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), TEquipment::getEquipmentName, bo.getEquipmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentImg()), TEquipment::getEquipmentImg, bo.getEquipmentImg());
        lqw.eq(StringUtils.isNotBlank(bo.getEdition()), TEquipment::getEdition, bo.getEdition());
        lqw.eq(StringUtils.isNotBlank(bo.getSoftware()), TEquipment::getSoftware, bo.getSoftware());
        lqw.eq(StringUtils.isNotBlank(bo.getOpsDeal()), TEquipment::getOpsDeal, bo.getOpsDeal());
        lqw.eq(StringUtils.isNotBlank(bo.getOpsYun()), TEquipment::getOpsYun, bo.getOpsYun());
        lqw.eq(StringUtils.isNotBlank(bo.getOpsNei()), TEquipment::getOpsNei, bo.getOpsNei());
        lqw.eq(StringUtils.isNotBlank(bo.getEqId()), TEquipment::getEqId, bo.getEqId());
        lqw.eq(StringUtils.isNotBlank(bo.getOther()), TEquipment::getOther, bo.getOther());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), TEquipment::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getSn()), TEquipment::getSn, bo.getSn());
        lqw.eq(bo.getStatus() != null, TEquipment::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getSubAddr()), TEquipment::getSubAddr, bo.getSubAddr());
        lqw.eq(bo.getDeptId() != null, TEquipment::getDeptId, bo.getDeptId());
        lqw.eq(bo.getCreateId() != null, TEquipment::getCreateId, bo.getCreateId());
        lqw.eq(bo.getX() != null, TEquipment::getX, bo.getX());
        lqw.eq(bo.getY() != null, TEquipment::getY, bo.getY());
        lqw.eq(StringUtils.isNotBlank(bo.getInstallAddress()), TEquipment::getInstallAddress, bo.getInstallAddress());
        return lqw;
    }

    /**
     * 新增设备
     */
    @Override
    public Boolean insertByBo(TEquipmentBo bo) {
        TEquipment add = BeanUtil.toBean(bo, TEquipment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备
     */
    @Override
    public Boolean updateByBo(TEquipmentBo bo) {
        TEquipment update = BeanUtil.toBean(bo, TEquipment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TEquipment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteWithValidById(Long id) {
        return baseMapper.deleteById(id)>0;
    }
}
